Today, you will load a filtered gapminder dataset - with a subset of data on global development from 1952 - 2007 in increments of 5 years - to capture the period between the Second World War and the Global Financial Crisis.
Your task: Explore the data and visualise it in both static and animated ways, providing answers and solutions to 7 questions/tasks below.
First, start with installing the relevant packages ‘tidyverse’, ‘gganimate’, and ‘gapminder’.
First, see which specific years are actually represented in the dataset and what variables are being recorded for each country. Note that when you run the cell below, Rmarkdown will give you two results - one for each line - that you can flip between.
unique(gapminder$year)
## [1] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 2002 2007
head(gapminder)
## # A tibble: 6 x 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
The dataset contains information on each country in the sampled year, its continent, life expectancy, population, and GDP per capita.
Let’s plot all the countries in 1952.
theme_set(theme_bw()) # set theme to white background for better visibility
ggplot(subset(gapminder, year == 1952), aes(gdpPercap, lifeExp, size = pop)) +
geom_point() +
scale_x_log10() +
labs(x = "GDP per capita", y = "Life Expectancy")
We see an interesting spread with an outlier to the right. Answer the following questions, please:
Q1. Why does it make sense to have a log10 scale on x axis?
It makes sense to have a log10 scale on the x-axis, because the x-axis covers a large range of values in which there is a great difference between the largest and the smallest values. Using a log10 scale means that we can actually avoid that small values cluster together in the bottom of the axis. This is because moving one unit on the x-axis means that the number is multiplied by 10. Logarithmic scales are often used when we are dealing with exponential growth. If we did not use a log scale, the values would increase too quickly and would not be able to fit, but using a log scale enables us to get a clear overview of an exponential growth.
Q2. What country is the richest in 1952 (far right on x axis)?
The richest country in 1952 was Kuwait (see below).
gapminder %>%
select(country, year, gdpPercap) %>% # selecting the variables of interest
group_by(country) %>% # grouping by country
filter(year == 1952) %>% # specifying the year 1952
arrange(desc(gdpPercap)) # arranging by GDP in descending order
## # A tibble: 142 x 3
## # Groups: country [142]
## country year gdpPercap
## <fct> <int> <dbl>
## 1 Kuwait 1952 108382.
## 2 Switzerland 1952 14734.
## 3 United States 1952 13990.
## 4 Canada 1952 11367.
## 5 New Zealand 1952 10557.
## 6 Norway 1952 10095.
## 7 Australia 1952 10040.
## 8 United Kingdom 1952 9980.
## 9 Bahrain 1952 9867.
## 10 Denmark 1952 9692.
## # … with 132 more rows
You can generate a similar plot for 2007 and compare the differences
ggplot(subset(gapminder, year == 2007), aes(gdpPercap, lifeExp, size = pop)) +
geom_point() +
scale_x_log10() +
labs(x = "GDP per capita", y = "Life Expectancy")
The black bubbles are a bit hard to read, the comparison would be easier with a bit more visual differentiation.
Q3. Can you differentiate the continents by color and fix the axis labels?
I can differentiate continents by color by adding color = continents to the aesthetics, and I can add axis labels with labs() (see below).
ggplot(subset(gapminder, year == 2007), aes(gdpPercap, lifeExp,size = pop, color = continent)) +
geom_point() +
scale_x_log10() +
labs(x = "GDP per capita", y = "Life Expectancy")
Q4. What are the five richest countries in the world in 2007?
The five richest countries in the world in 2007 were Norway, Kuwait, Singapore, United States, and Ireland (see below)l
gapminder %>%
select(country, year, gdpPercap) %>% # selecting the variables of interest
group_by(country) %>% # grouping by country
filter(year == 2007) %>% # specifying the year 2007
arrange(desc(gdpPercap)) %>% # arranging by GDP in descending order
head(n = 5) # taking the top 5
## # A tibble: 5 x 3
## # Groups: country [5]
## country year gdpPercap
## <fct> <int> <dbl>
## 1 Norway 2007 49357.
## 2 Kuwait 2007 47307.
## 3 Singapore 2007 47143.
## 4 United States 2007 42952.
## 5 Ireland 2007 40676.
The comparison would be easier if we had the two graphs together, animated. We have a lovely tool in R to do this: the gganimate package. And there are two ways of animating the gapminder ggplot.
The first step is to create the object-to-be-animated
anim <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop)) +
geom_point() +
scale_x_log10() + # convert x to log scale
labs(x = "GDP per capita", y = "Life Expectancy")
anim
This plot collates all the points across time. The next step is to split it into years and animate it. This may take some time, depending on the processing power of your computer (and other things you are asking it to do). Beware that the animation might appear in the ‘Viewer’ panel, not in this rmd preview. You need to knit the document to get the viz inside an html file.
anim + transition_states(year,
transition_length = 1,
state_length = 1)
Notice how the animation moves jerkily, ‘jumping’ from one year to the next 12 times in total. This is a bit clunky, which is why it’s good we have another option.
This option smoothes the transition between different ‘frames’, because it interpolates and adds transitional years where there are gaps in the timeseries data.
anim2 <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop)) +
geom_point() +
scale_x_log10() + # convert x to log scale
transition_time(year) +
labs(x = "GDP per capita", y = "Life Expectancy")
anim2
The much smoother movement in Option 2 will be much more noticeable if you add a title to the chart, that will page through the years corresponding to each frame.
Q5 Can you add a title to one or both of the animations above that will change in sync with the animation? [hint: search labeling for transition_states() and transition_time() functions respectively]
Animation 1:
anim + transition_states(year,
transition_length = 1,
state_length = 1) +
labs(title = "Year: {closest_state}") # adding a dynamic title displaying the year
Animation 2:
anim2 <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop)) +
geom_point() +
scale_x_log10() +
transition_time(year) +
labs(title = "Year: {frame_time}") + # adding a dynamic title displaying the year
labs(x = "GDP per capita", y = "Life Expectancy")
anim2
Q6 Can you make the axes’ labels and units more readable? Consider expanding the abbreviated labels as well as the scientific notation in the legend and x axis to whole numbers.[hint:search disabling scientific notation]
Animation 1: I have added readable labels for the x- and y-axis as well as a more readable title of the legend using the labs() function. I have also disabled the scientific notation both from the x-axis and the legend using the labels = comma option inside the scale_x_log10 function to get the GDP per capita in whole numbers and the option(scipen = 999) to get the population estimate in the legend in whole numbers (see below)
anim <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, options(scipen = 999))) +
geom_point() +
scale_x_log10(labels = comma) +
labs(x = "GDP per capita", y = "Life Expectancy", size = "Population")
anim + transition_states(year,
transition_length = 1,
state_length = 1) +
labs(title = "Year: {closest_state}")
Animation 2: Same procedure as above.
anim2 <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, options(scipen = 999))) +
geom_point() +
scale_x_log10(labels = comma) +
labs(x = "GDP per capita", y = "Life Expectancy") +
transition_time(year) +
labs(title = "Year: {frame_time}")
anim2
Q7 Come up with a question you want to answer using the gapminder data and write it down. Then, create a data visualisation that answers the question and explain how your visualization answers the question. (Example: you wish to see what was mean life expectancy across the continents in the year you were born versus your parents’ birth years). [hint: if you wish to have more data than is in the filtered gapminder, you can load either the gapminder_unfiltered dataset and download more at https://www.gapminder.org/data/ ]
Question: What is the correlation between GDP per capita and life expectancy across continents in the year 1997?
Answer: There is a positive correlation between GDP per capita and life expectancy across continents in the year 1997 as displayed in the visualization below. Hence, the higher the GDP, the higher the life expectancy. Of course there are some exceptions. Furthermore, we can see that African countries generally have a low GDP as well as a low life expectancy in the year 1997 while Europe, America, Oceania, and some Asian countries have a higher GDP per capita as well as a higher life expectancy.
ggplot(subset(gapminder, year == 1997), aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point() +
scale_x_log10() +
labs(x = "GDP per capita", y = "Life Expectancy")